﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderManagement;
using System.IO;

namespace OrderWinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.InitAll();
            Order_bindingSource.DataSource = orders;
            Order order = Order_bindingSource.Current as Order;
            goodsBindingSource.DataSource = order.OrderDetails.goods;
           

            
        }
        
        public  List<Order> orders;
        public  OrderService service;
        public  void InitAll()
        {
            Customers customers1 = new Customers("张小亮", "C3-224", "110");
            Customers customers2 = new Customers("渣渣诚", "C3-319", "119");
            Customers customers3 = new Customers("李小奥","C3-225", "120");
            Goods good1 = new Goods("康师傅泡面", 4.5, 12, 2); // 9
            Goods good2 = new Goods("泡面搭档火腿肠", 2, 12, 3); //6
            Goods good3 = new Goods("果粒橙", 4, 12, 1); //4
            Goods good4 = new Goods("营养快线", 5, 12, 2);//10
            Goods good5 = new Goods("可口可乐", 4, 12, 3);
            Goods good6 = new Goods("百事可乐", 3, 12, 2);
            List<Goods> goods1 = new List<Goods>();
            List<Goods> goods2 = new List<Goods>();
            List<Goods> goods3 = new List<Goods>();
            goods1.Add(good1);
            goods1.Add(good2);

            goods2.Add(good3);
            goods2.Add(good4);

            goods3.Add(good5);
            goods3.Add(good6);
            OrderDetails orderDetails1 = new OrderDetails(goods1);
            OrderDetails orderDetails2 = new OrderDetails(goods2);
            OrderDetails orderDetails3 = new OrderDetails(goods3);
            //生成测试订单
            Order order1 = new Order(customers1, orderDetails1, "000001");
            Order order2 = new Order(customers2, orderDetails2, "000002");
            Order order3 = new Order(customers3, orderDetails3, "000003");
            
            //初始化订单列表
            orders = new List<Order>();
            orders.Add(order1);
            orders.Add(order2);
            orders.Add(order3);
            //this.orders.Add(order4);
            //初始化控制器
            service = new OrderService(orders);

    }
        //ID订单查询
        private void ID_button_Click(object sender, EventArgs e)
        {
            String ID = CheckID.Text;
            try
            {
                List<Order> Checkorders = service.OrderCheckID(ID);
            }
            catch(Exception m)
            {
                MessageBox.Show(m.Message);
            }
            Order_bindingSource.DataSource= service.OrderCheckID(ID);

        }

        //取消按键
        private void Cancle_button_Click(object sender, EventArgs e)
        {
            Order_bindingSource.DataSource = orders;
        }

        //OrderqueryByname
        private void Name_button_Click(object sender, EventArgs e)
        {
            String name = text_name.Text;
            try
            {
                List<Order> Check_orders = service.OrderCheckName(name);
            }
            catch(Exception m)
            {
                MessageBox.Show(m.Message);
            }
            Order_bindingSource.DataSource = service.OrderCheckName(name);
        }

        //OrderqueryByPrice
        private void Price_button_Click(object sender, EventArgs e)
        {
            int min = Convert.ToInt32(text_min.Text);
            int max = Convert.ToInt32(text_max.Text);
            try
            {
                List<Order> Check_Orders =service.OrderCheckPrice(min, max);
            }
            catch(Exception m)
            {
                MessageBox.Show(m.Message);
            }
            Order_bindingSource.DataSource= service.OrderCheckPrice(min, max);
        }
        //删除订单
        public void Delete_button_Click(object sender, EventArgs e) //异常处理
        {
            try
            {
                Order CurrentOrder = Order_bindingSource.Current as Order;
            }
            catch(Exception m)
            {
                MessageBox.Show("请选中有效行");
            }

            Order CurrentOrder1 = Order_bindingSource.Current as Order;
            this.Order_bindingSource.Remove(CurrentOrder1);
        }

        private void Order_bindingSource_CurrentChanged(object sender, EventArgs e)   //添加异常处理
        {

            Order order = Order_bindingSource.Current as Order;
            List<Goods> list = new List<Goods>();

            try
            {
                goodsBindingSource.DataSource = order.OrderDetails.goods;
            }
            catch (Exception m)
            {
                MessageBox.Show("请选择有效行");
            }




        }

        private void Export_button_Click(object sender, EventArgs e)
        {
            Stream stream;
            if(saveFileDialog1.ShowDialog() == DialogResult.OK && (stream = saveFileDialog1.OpenFile()) != null)
            {
                service.Export(stream);
                stream.Close();
            }
        }

        private void Import_button_Click(object sender, EventArgs e)
        {
            Stream stream;
            if (openFileDialog1.ShowDialog() == DialogResult.OK && (stream = openFileDialog1.OpenFile()) != null)
            {
                try
                {
                    List<Order> list = service.Import(stream);
                    foreach(Order m in list)
                    {
                        if (!service.IsHave(m))
                        {
                           service.AddOrder(m);
                        }
                    }
                }catch(Exception m)
                {
                    MessageBox.Show("订单错误");
                }
                finally
                {
                    stream.Close();
                }
            }

        }

        private void Create_button_Click(object sender, EventArgs e)
        {
            OrderCreate form = new OrderCreate(this);
            form.ShowDialog();
        }

        private void Modify_button_Click(object sender, EventArgs e)
        {
            Order Current = Order_bindingSource.Current as Order;
            ModifyOrder form = new ModifyOrder(this, Current);
            form.ShowDialog();
        }
    }
}
